package cn.piaoyi.web.action;

import java.util.Map;

import cn.piaoyi.bean.Admin;
import cn.piaoyi.comm.Constant;
import cn.piaoyi.comm.Util;
import cn.piaoyi.service.AdminService;
import cn.piaoyi.web.servlet.AdminSession;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.SessionAware;

public class AdminLogin extends BaseAction implements SessionAware {
	private static final Log logger = LogFactory.getLog(AdminLogin.class);
	
	private AdminService adminService;

	public AdminService getAdminService() {
		return adminService;
	}

	public void setAdminService(AdminService adminService) {
		this.adminService = adminService;
	}

	private Map session;

	public void setSession(Map session) {
		this.session = session;
	}

	public Map getSession() {
		return session;
	}
	
	private String username;
	private String password;
	private String verifycode;
	
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	public String getVerifycode() {
		return verifycode;
	}

	public void setVerifycode(String verifycode) {
		this.verifycode = verifycode;
	}

	public String execute() {
		try {
			return this.executeMethod(this.getAction());
		} catch (Exception e) {
			logger.error(e);
			return INPUT;
		}
	}

	/**
	 * 初始页
	 */
	public String index(){
		this.setAction("login");
		return "login";
	}
	
	public String login(){
		logger.info(Util.hash(this.password));
		//验证码正确
		if(!getSessionAuthCode().equalsIgnoreCase(verifycode)){
			this.addFieldError("username", this.getText("error.autherror"));
			//this.addActionError(this.getText("error.nullerror"));
			return "login";
		}
		//判断用户名和密码是否为空
		if(StringUtils.isBlank(this.username) || StringUtils.isBlank(this.password)) {
			this.addFieldError("username", this.getText("error.nullerror"));
			//this.addActionError(this.getText("error.nullerror"));
			return "login";
		}
		Admin a = this.getAdminService().findAdminByUsername(this.username);
		if(a == null) {
			this.addFieldError("username", this.getText("error.nullerror"));
			return "login";
		}
		//判断密码
		if(!Util.hash(this.password).equalsIgnoreCase(a.getPassword())){
			this.addFieldError("username", this.getText("error.nullerror"));
			return "login";
		}
		AdminSession as = new AdminSession();
		as.setId(a.getId());
		as.setUsername(a.getUsername());
		this.getSession().put(Constant.ADMIN_SESSION_KEY, as);
		return "admin_index";
	}
	
	private String getSessionAuthCode() {
        String auth = (String) this.getSession().get("authCode");
        if(StringUtils.isBlank(auth)){
        	return "";
        }else{
        	return auth;
        }
    }
	
}
